找传奇、传世资源到传世资源站!

Go语言程序设计

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

go语言程序设计,经典书箱,入门的好资料!本书由《C程序设计语言》的作者Kernighan和谷歌公司Go团队主管Alan Donovan联袂撰写,是学习Go语言程序设计的指南。本书共13章,主要内容包括:Go的基础知识、基本结构、基本数据类型、复合数据类型、函数、方法、接口、goroutine、通道、共享变量的并发性、包、go工具、测试、反射等。from clipboardContentsPreface................................................................................................................................ xixPART 1—WHY LEARN GO—GETTING STARTEDChapter 1—Origins, Context and Popularity of Go..............................................................11.1 Origins and evolution ...............................................................................................11.2 Main characteristics, context and reasons for developing a new language...................41.2.1 Languages that influenced Go........................................................................41.2.2 Why a new language?.....................................................................................51.2.3 Targets of the language...................................................................................51.2.4 Guiding design principles ..............................................................................71.2.5 Characteristics of the language.......................................................................71.2.6 Uses of the language.......................................................................................81.2.7 Missing features?............................................................................................91.2.8 Programming in Go.....................................................................................101.2.9 Summary .....................................................................................................10Chapter 2—Installation and Runtime Environment ...........................................................112.1 Platforms and architectures ....................................................................................11(1) The gc Go-compilers: .................................................................................11(2) The gccgo-compiler:...................................................................................13(3) File extensions and packages: ......................................................................142.2 Go Environment variables.......................................................................................142.3 Installing Go on a Linux system..............................................................................162.4 Installing Go on an OS X system ............................................................................212.5 Installing Go on a Windows system.........................................................................212.6 What is installed on your machine? ........................................................................262.7 The Go runtime......................................................................................................272.8 A Go interpreter .....................................................................................................27Chapter 3—Editors, IDE’s and Other tools.........................................................................283.1 Basic requirements for a decent Go development environment................................283.2 Editors and Integrated Development Environments................................................293.2.1. Golang LiteIDE .........................................................................................323.2.2. GoClipse.....................................................................................................333.3 Debuggers...............................................................................................................343.4 Building and running go-programs with command- and Makefiles.........................353.5 Formatting code: go fmt or gofmt ...........................................................................393.6 Documenting code: go doc or godoc.......................................................................403.7 Other tools..............................................................................................................413.8 Go’s performance ....................................................................................................413.9 Interaction with other languages..............................................................................433.9.1. Interacting with C ......................................................................................433.9.2. Interacting with C ...................................................................................45PART 2—CORE CONSTRUCTS AND TECHNIQUES OF THE LANGUAGEChapter 4—Basic constructs and elementary data types ......................................................494.1. Filenames—Keywords—Identifiers.........................................................................494.2. Basic structure and components of a Go-program ..................................................504.2.1 Packages, import and visibility .....................................................................514.2.3 Comments...................................................................................................564.2.4 Types............................................................................................................574.2.5 General structure of a Go-program ..............................................................584.2.6 Conversions.................................................................................................604.2.7 About naming things in Go .........................................................................604.3. Constants...............................................................................................................604.4. Variables.................................................................................................................634.4.1 Introduction ................................................................................................634.4.2 Value types and reference types ....................................................................664.4.3 Printing........................................................................................................684.4.4 Short form with the := assignment operator.................................................694.4.5 Init-functions...............................................................................................704.5. Elementary types and operators..............................................................................734.5.1. Boolean type bool .......................................................................................734.5.2. Numerical types..........................................................................................754.5.2.1 ints and floats............................................................................................754.5.2.2 Complex numbers ....................................................................................794.5.2.3 Bit operators .............................................................................................794.5.2.4 Logical operators.......................................................................................814.5.2.5 Arithmetic operators ................................................................................824.5.2.6 Random numbers .....................................................................................824.5.3. Operators and precedence...........................................................................844.5.4. Aliasing types..............................................................................................844.5.5. Character type ............................................................................................854.6. Strings....................................................................................................................864.7. The strings and strconv package.............................................................................884.7.1—Prefixes and suffixes:..................................................................................884.7.2—Testing whether a string contains a substring:............................................894.7.3—Indicating at which position (index) a substring or character occursin a string: ..................................................................................................894.7.4—Replacing a substring: ...............................................................................904.7.5—Counting occurrences of a substring:.........................................................904.7.6—Repeating a string:.....................................................................................904.7.7—Changing the case of a string:....................................................................914.7.8—Trimming a string: ....................................................................................924.7.9—Splitting a string:.......................................................................................924.7.10—Joining over a slice: .................................................................................924.7.11—Reading from a string:.............................................................................934.8. Times and dates......................................................................................................954.9. Pointers..................................................................................................................96Chapter 5—Control structures..........................................................................................1015.1—The if else construct ...........................................................................................1015.2—Testing for errors on functions with multiple return values.................................1065.3—The switch keyword ...........................................................................................1105.4—The for construct ...............................................................................................1145.4.1 Counter-controlled iteration ......................................................................114Character on position 2 is: ..........................................................................................1165.4.2 Condition-controlled iteration ..................................................................1175.4.3 Infinite loops ............................................................................................1185.4.4 The for range construct..............................................................................1195.5—Break / continue.................................................................................................1215.6—Use of labels with break and continue—goto......................................................123Chapter 6—Functions.......................................................................................................1266.1 Introduction..........................................................................................................1266.2 Parameters and return values .................................................................................1296.2.1 Call by value / Call by reference.................................................................1296.2.2 Named return variables..............................................................................1316.2.3 Blank identifier..........................................................................................1336.2.4 Changing an outside variable.....................................................................1346.3 Passing a variable number of parameters................................................................1356.4 Defer and tracing ..................................................................................................1376.5 Built-in functions..................................................................................................1426.6 Recursive functions ...............................................................................................1436.8 Closures (function literals) ....................................................................................1476.9 Applying closures: a function returning another function .....................................1506.10 Debugging with closures .....................................................................................1536.11 Timing a function ..............................................................................................1546.12 Using memoization for performance...................................................................154Chapter 7—Arrays and Slices............................................................................................1577.1 Declaration and initialization ................................................................................1577.1.1 Concept.....................................................................................................1577.1.2 Array literals...............................................................................................1617.1.3 Multidimensional arrays.............................................................................1627.1.4 Passing an array to a function.....................................................................1637.2 Slices.....................................................................................................................1647.2.1 Concept.....................................................................................................1647.2.2 Passing a slice to a function........................................................................1687.2.3 Creating a slice with make().......................................................................1687.2.4 Difference between new() and make()........................................................1707.2.5 Multidimensional slices..............................................................................1717.2.6 The bytes package......................................................................................1717.3 For range construct ...............................................................................................1727.4 Reslicing................................................................................................................1757.5 Copying and appending slices ...............................................................................1767.6 Applying strings, arrays and slices..........................................................................1787.6.1 Making a slice of bytes from a string ..........................................................1787.6.2 Making a substring of a string....................................................................1797.6.3 Memory representation of a string and a slice.............................................1797.6.4 Changing a character in a string.................................................................1807.6.5 Comparison function for byte arrays..........................................................1807.6.6 Searching and sorting slices and arrays ......................................................1817.6.7 Simulating operations with append............................................................1827.6.8 Slices and garbage collection ......................................................................182Chapter 8—Maps .............................................................................................................1858.1 Declaration, initialization and make......................................................................1858.1.1 Concept.....................................................................................................1858.1.2 Map capacity .............................................................................................1888.1.3 Slices as map values....................................................................................1888.2 Testing if a key-value item exists in a map—Deleting an element ..........................1888.3 The for range construct .........................................................................................1908.4 A slice of maps .....................................................................................................1918.5 Sorting a map........................................................................................................1928.6 Inverting a map.....................................................................................................194Chapter 9—Packages ........................................................................................................196A The standard library.................................................................................................1969.1 Overview of the standard library............................................................................1969.2 The regexp package. ..............................................................................................1999.3 Locking and the sync package. ..............................................................................2009.4 Accurate computations and the big package. .........................................................202B Custom and external packages: use, build, test, document, install ............................2039.5 Custom packages and visibility..............................................................................2039.6 Using godoc for your custom packages..................................................................2089.7 Using go install for installing custom packages. .....................................................2109.8 Custom packages: map structure, go install and go test .........................................2129.8.1 Map-structure for custom packages............................................................2129.8.2 Locally installing the package.....................................................................2159.8.3 OS dependent code....................................................................................2169.9 Using git for distribution and installation..............................................................2169.9.1 Installing to github ....................................................................................2169.9.2 Installing from github ................................................................................2179.10 Go external packages and projects. .....................................................................2189.11 Using an external library in a Go program...........................................................219Chapter 10—Structs and Methods....................................................................................22410.1 Definition of a struct...........................................................................................22410.2 Creating a struct variable with a Factory method.................................................23210.2.1 A factory for structs..................................................................................23210.2.2 new() and make() revisited for maps and structs:......................................23410.3 Custom package using structs..............................................................................23510.4 Structs with tags..................................................................................................23610.5 Anonymous fields and embedded structs.............................................................23710.5.1 Definition................................................................................................23710.5.2 Embedded structs ....................................................................................23810.5.3 Conflicting names....................................................................................23910.6 Methods..............................................................................................................24010.6.1 What is a method? ...................................................................................24010.6.2 Difference between a function and a method ...........................................24410.6.3 Pointer or value as receiver.......................................................................24510.6.4 Methods and not-exported fields.............................................................24710.6.5 Methods on embedded types and inheritance...........................................24810.6.6 How to embed functionality in a type......................................................25110.6.7 Multiple inheritance.................................................................................25310.6.8 Universal methods and method naming...................................................25610.6.9 Comparison between Go types and methods and otherobject-oriented languages..........................................................................25610.7 The String()-method and format specifiers for a type...........................................25810.8 Garbage collection and SetFinalizer.....................................................................261Chapter 11—Interfaces and reflection...............................................................................26311.1 What is an interface? ...........................................................................................26311.2 Interface embedding interface(s)..........................................................................27011.3 How to detect and convert the type of an interface variable: type assertions ........27011.4 The type switch...................................................................................................27311.5 Testing if a value implements an interface............................................................27411.6 Using method sets with interfaces........................................................................27511.7 1st example: sorting with the Sorter interface.......................................................27711.8 2nd example: Reading and Writing .....................................................................28211.9 Empty Interface...................................................................................................28411.9.1 Concept...................................................................................................28411.9.2 Constructing an array of a general type or with variables ofdifferent types...........................................................................................28611.9.3 Copying a data-slice in a slice of interface{}..............................................28711.9.4 Node structures of general or different types ............................................28811.9.5 Interface to interface................................................................................28911.10 The reflect package............................................................................................29011.10.1 Methods and types in reflect ..................................................................29011.10.2 Modifying (setting) a value through reflection........................................29311.10.3 Reflection on structs ..............................................................................29411.11 Printf and reflection. .........................................................................................29611.12 Interfaces and dynamic typing...........................................................................29811.12.1 Dynamic typing in Go...........................................................................29811.12.2 Dynamic method invocation..................................................................30011.12.3 Extraction of an interface.......................................................................30111.12.4 Explicitly indicating that a type implements an interface........................30311.12.5 Empty interface and function overloading..............................................30411.12.6 Inheritance of interfaces.........................................................................30411.13 Summary: the object-orientedness of Go...........................................................30611.14 Structs, collections and higher order functions ..................................................306PART 3—ADVANCED GOChapter 12—Reading and writing ....................................................................................31312.1 Reading input from the user................................................................................31312.2 Reading from and writing to a file.......................................................................31712.2.1 Reading from a file...................................................................................31712.2.2 The package compress: reading from a zipped file....................................32112.2.3 Writing to a file........................................................................................32212.3 Copying files.......................................................................................................32412.4 Reading arguments from the command-line........................................................32512.4.1 With the os-package.................................................................................32512.4.2 With the flag-package..............................................................................32612.5 Reading files with a buffer...................................................................................32812.6 Reading and writing files with slices....................................................................33012.7 Using defer to close a file....................................................................................33212.8 A practical example of the use of interfaces: fmt.Fprintf .....................................33212.9 The json dataformat ............................................................................................33412.10 The xml dataformat...........................................................................................34012.11 Datatransport through gob................................................................................34212.12 Cryptography with go .......................................................................................345Chapter 13—Error-handling and Testing..........................................................................34813.1 Error-handling ....................................................................................................34913.1.1 Defining errors.........................................................................................34913.1.2 Making an error-object with fmt.............................................................35313.2 Run-time exceptions and panic ...........................................................................35313.4 Error-handling and panicking in a custom package.............................................35713.5 An error-handling scheme with closures ..............................................................36013.6 Starting an external command or program ..........................................................36313.7 Testing and benchmarking in Go ........................................................................36413.8 Testing: a concrete example.................................................................................36713.9 Using table-driven tests. ......................................................................................36913.10 Investigating performance: tuning and profiling Go programs...........................37113.10.1 Time and memory consumption............................................................37113.10.2 Tuning with go test ................................................................................37113.10.3 Tuning with pprof..................................................................................371Chapter 14—Goroutines and Channels ............................................................................37514.1 Concurrency, parallelism and goroutines.............................................................37514.1.1 What are goroutines? ...............................................................................37514.1.2 The difference between concurrency and parallelism................................37714.1.3 Using GOMAXPROCS...........................................................................37814.1.4 How to specify the number of cores to be used on the command-line?.....37914.1.5 Goroutines and coroutines.......................................................................38114.2 Channels for communication between goroutines...............................................38114.2.1 Concept...................................................................................................38114.2.2 Communication operator <- ....................................................................38314.2.3 Blocking of channels................................................................................38514.2.4 Goroutines synchronize through the exchange of data on one (ormore) channel(s).......................................................................................38714.2.5 Asynchronous channels—making a channel with a buffer........................38714.2.6 Goroutine using a channel for outputting result(s)...................................38814.2.7 Semaphore pattern...................................................................................38914.2.8 Implementing a parallel for-loop..............................................................39114.2.9 Implementing a semaphore using a buffered channel ...............................39114.2.10 For—range applied to channels..............................................................39414.2.11 Channel directionality............................................................................39614.3 Synchronization of goroutines: closing a channel—testing for blocked channels .40014.4 Switching between goroutines with select............................................................40314.5 Channels, Timeouts and Tickers..........................................................................40814.6 Using recover with goroutines .............................................................................41214.7 Comparing the old and the new model: Tasks and Worker processes...................41314.8 Implementing a lazy generator.............................................................................41614.9 Implementing Futures.........................................................................................42014.10 Multiplexing .....................................................................................................42114.10.1 A typical client-server pattern.................................................................42114.10.2 Teardown: shutdown the server by signaling a channel...........................42414.11 Limiting the number of requests processed concurrently ...................................42714.12 Chaining goroutines..........................................................................................42814.13 Parallelizing a computation over a number of cores ...........................................42914.14 Parallelizing a computation over a large amount of data ....................................43014.15 The leaky bucket algorithm...............................................................................43114.16 Benchmarking goroutines..................................................................................43314.17 Concurrent acces to objects by using a channel. ................................................434Chapter 15—Networking, templating and web-applications.............................................43615.1 A tcp-server ........................................................................................................43615.2 A simple webserver..............................................................................................44515.3 Polling websites and reading in a web page..........................................................44815.4 Writing a simple web application ........................................................................45215.5 Making a web application robust.........................................................................45415.6 Writing a web application with templates............................................................45615.7 Exploring the template package...........................................................................46115.7.1. Field substitution: {{.FieldName}}...........................................................46215.7.2. Validation of the templates......................................................................46315.7.3 If-else.......................................................................................................46415.7.4 Dot and with-end ....................................................................................46515.7.5 Template variables $.................................................................................46615.7.6 Range-end................................................................................................46715.7.7 Predefined template functions..................................................................46715.8 An elaborated webserver with different functions ................................................468(works only on Unix because calls /bin/date).......................................................47415.9 Remote procedure calls with rpc..........................................................................47415.10 Channels over a network with netchan..............................................................47715.11 Communication with websocket.......................................................................47815.12 Sending mails with smtp ...................................................................................480PART 4—APPLYING GOChapter 16—Common Go Pitfalls or Mistakes.................................................................48516.1 Hiding (shadowing) a variable by misusing short declaration...............................48616.2 Misusing strings. .................................................................................................48616.3 Using defer for closing a file in the wrong scope..................................................48716.4 Confusing new() and make()...............................................................................48816.5 No need to pass a pointer to a slice to a function.................................................48816.6 Using pointers to interface types..........................................................................48816.7 Misusing pointers with value types......................................................................48916.8 Misusing goroutines and channels.......................................................................48916.9 Using closures with goroutines ............................................................................49016.10 Bad error handling ............................................................................................49116.10.1 Don’t use booleans: ................................................................................49116.10.2 Don’t clutter your code with error-checking:..........................................492Chapter 17—Go Language Patterns..................................................................................49417.1 The comma, ok pattern.......................................................................................49417.2 The defer pattern.................................................................................................49517.3 The visibility pattern ...........................................................................................49717.4 The operator pattern and interface......................................................................49717.4.1 Implement the operators as functions.......................................................49717.4.2 Implement the operators as methods........................................................49817.4.3 Using an interface....................................................................................499Chapter 18—Useful Code Snippets—Performance Advice................................................50018.1 Strings.................................................................................................................50018.2 Arrays and slices ..................................................................................................50118.3 Maps...................................................................................................................50218.4 Structs.................................................................................................................50218.5 Interfaces.............................................................................................................50318.6 Functions............................................................................................................50318.7 Files.....................................................................................................................50418.8 Goroutines and channels.....................................................................................50518.9 Networking and web applications........................................................................50718.9.1. Templating: .....................................................................................................50718.10 General .............................................................................................................50818.11 Performance best practices and advice ...............................................................508Chapter 19—Building a complete application...................................................................50919.1 Introduction........................................................................................................50919.2 Introducing Project UrlShortener........................................................................50919.3 Data structure.....................................................................................................51019.4 Our user interface: a web server frontend ............................................................51519.5 Persistent storage: gob .........................................................................................51919.6 Using goroutines for performance.......................................................................52419.7 Using json for storage..........................................................................................52719.8 Multiprocessing on many machines.....................................................................52819.9 Using a ProxyStore..............................................................................................53219.10 Summary and enhancements.............................................................................536Chapter 20—Go in Google App Engine...........................................................................53820.1 What is Google App Engine ?..............................................................................53820.2 Go in the cloud ..................................................................................................54020.3 Installation of the Go App Engine SDK: the development environment for Go ..54020.3.1. Installation..............................................................................................54020.3.2. Checking and testing ..............................................................................54220.4 Building your own Hello world app ...................................................................54320.4.1 Map structure—Creating a simple http-handler.......................................54320.4.2 Creating the configuration file app.yaml ..................................................54420.4.3 Iterative development...............................................................................54820.4.4. Integrating with the GoClipse IDE.........................................................54820.5 Using the Users service and exploring its API ......................................................54920.6 Handling forms...................................................................................................55120.7 Using the datastore..............................................................................................55220.8 Uploading to the cloud ......................................................................................556Chapter 21—Real World Uses of Go ................................................................................55921.1 Heroku—a highly available consistent data store in Go. .....................................55921.2 MROffice—a VOIP system for call centers in Go. ..............................................56121.3 Atlassian—a virtual machine cluster management system....................................56221.4 Camlistore—a content addressable storage system...............................................56321.5 Other usages of the Go language. ........................................................................563APPENDICES..................................................................................................................567(A) CODE REFERENCE ..........................................................................................567(B)CUTE GO QUOTES............................................................................................571GO QUOTES: TRUE BUT NOT SO CUTE...................................................572(C) LIST OF CODE EXAMPLES (Listings)..............................................................572(E) References in the text to Go—packages.................................................................583(F) References in the text to Go—tools .......................................................................586(G) Answers to Questions ...........................................................................................586(H) ANSWERS TO EXERCISES...............................................................................590(I) BIBLIOGRAPHY (Resources and References).......................................................593Index.............................................................................................................................597

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复